﻿// -----------------------------------------------------------------------
// <copyright file="ImageContainerForm.cs" company="FCIS">
// You are welcome to do whatever you want with the code in this file
// </copyright>
// -----------------------------------------------------------------------

namespace ImagePackage.WindowsForms
{
    using System;
    using System.ComponentModel;
    using System.Windows.Forms;

    /// <summary>
    /// Image form
    /// </summary>
    public partial class ImageContainerForm : Form
    {
        /// <summary>
        /// the image container
        /// </summary>
        private ImageContainer theImageContainer;

        /// <summary>
        /// current model
        /// </summary>
        private Model.Image current;

        /// <summary>
        /// Initializes a new instance of the <see cref="ImageContainerForm"/> class.
        /// </summary>
        /// <param name="current">The current.</param>
        public ImageContainerForm(Model.Image current)
        {
            this.InitializeComponent();

            this.current = current;

            this.theImageContainer = new ImageContainer { Image = current };

            this.DisplayImageContainer(this.theImageContainer);
        }

        /// <summary>
        /// Handles the Click event of the saveToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void SaveToolStripMenuItemClick(object sender, System.EventArgs e)
        {
            this.Save();
        }

        /// <summary>
        /// Handles the Click event of the saveAsToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void SaveAsToolStripMenuItemClick(object sender, System.EventArgs e)
        {
            this.SaveAs();
        }

        /// <summary>
        /// Invert tool strip menu item click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void InvertToolStripMenuItemClick(object sender, System.EventArgs e)
        {
            this.Invert();
        }

        /// <summary>
        /// Brightnesses the contrast tool strip menu item click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BrightnessContrastToolStripMenuItemClick(object sender, System.EventArgs e)
        {
            this.BrightnessContrast();
        }

        /// <summary>
        /// Saves this instance.
        /// </summary>
        private void Save()
        {
            this.SaveAs();
        }

        /// <summary>
        /// Saves as.
        /// </summary>
        private void SaveAs()
        {
            if (this.sfdMain.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show(@"Saving");
            }
        }

        /// <summary>
        /// Inverts this instance.
        /// </summary>
        private void Invert()
        {
            Core.PixelOperations.Parallel.Invert invert = new Core.PixelOperations.Parallel.Invert(this.theImageContainer.Image);

            this.theImageContainer.Image = invert.Apply();

            this.DisplayImageContainer(this.theImageContainer);
        }

        /// <summary>
        /// Brightnesses the contrast.
        /// </summary>
        private void BrightnessContrast()
        {
            LivePreviewInfo brightnessInfo = new LivePreviewInfo
            {
                Label = "Brightness",
                Value = 0,
                MaxValue = 127,
                MinValue = -127
            };

            LivePreviewInfo contrastInfo = new LivePreviewInfo
            {
                Label = "Contrast",
                Value = 0,
                MaxValue = 100,
                MinValue = -100
            };

            LivePreviewDialog liveDialog = new LivePreviewDialog(brightnessInfo, contrastInfo);

            Model.Image original = this.theImageContainer.Image;
            Core.PixelOperations.Parallel.Brightness brightness = new Core.PixelOperations.Parallel.Brightness(this.theImageContainer.Image);

            liveDialog.PropertyChanged += delegate(object sender, PropertyChangedEventArgs args)
            {
                switch (args.PropertyName)
                {
                    case "Brightness":
                        {
                            brightness.BrightnessValue = (sbyte)liveDialog.GetValue("Brightness");
                            this.theImageContainer.Image = brightness.Apply();
                            this.DisplayImageContainer(this.theImageContainer);
                        }

                        break;
                }
            };

            this.theImageContainer.Image = liveDialog.ShowDialog() == DialogResult.OK ? brightness.Apply() : original;
            this.DisplayImageContainer(this.theImageContainer);
        }

        /// <summary>
        /// Displays the current image container
        /// </summary>
        /// <param name="imageContainer">the image container object that has contains the model</param>
        private void DisplayImageContainer(ImageContainer imageContainer)
        {
            this.picImage.Image = imageContainer.Image.ToBitmap();
        }

        /// <summary>
        /// Images the container form size changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ImageContainerFormSizeChanged(object sender, EventArgs e)
        {
            // Nothing yet  
        }
    }
}
